import com.sun.xml.internal.bind.v2.util.QNameMap;

import java.io.*;
import java.util.*;

public class FileUtil {
    public Map<String,Integer> readFile(){
        String inPath = "/Users/shaozhenyu/Downloads/name-all.txt";
        Map<String,Integer> map = new HashMap<>();
        FileInputStream fileInput = null;
        try {
            File file = new File(inPath);
            StringBuffer sb = new StringBuffer();
            byte[] buffer = new byte[1024];
            fileInput = new FileInputStream(file);
            int byteread = 0;
            // byteread表示一次读取到buffers中的数量。
            while ((byteread = fileInput.read(buffer)) != -1) {
                //System.out.write(buffer, 0, byteread);
                String s = new String(buffer);
                if (s.length()>0){
                    sb.append(s);

                }
            }
            String[] result = sb.toString().split("\\s+");
            for (int i = 0;i<result.length;i++){
                if (map.get(result[i])!=null){
                    map.put(result[i].toUpperCase(),map.get(result[i])+1);
                }else{
                    map.put(result[i].toUpperCase(),1);
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {

            try {
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return map;

    }


    public Map<String,Integer> getDistinctStatus(){
        String inPath = "/Users/shaozhenyu/Downloads/working_status.txt";
        Map<String,Integer> map = new HashMap<>();
        FileInputStream fileInput = null;
        StringBuffer sb = new StringBuffer();
        try {
            File file = new File(inPath);

            byte[] buffer = new byte[1024];
            fileInput = new FileInputStream(file);
            int byteread = 0;
            // byteread表示一次读取到buffers中的数量。
            while ((byteread = fileInput.read(buffer)) != -1) {
                //System.out.write(buffer, 0, byteread);
                String s = new String(buffer);
                if (s.length()>0){
                    sb.append(s);
                }

            }
            String[] result = sb.toString().split("\\s+");
            for (int i = 0;i<result.length;i++){
                if (map.get(result[i])==null){
                    map.put(result[i].toUpperCase(),1);
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {

            try {
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return map;
    }

    public void writeFile(Map<String,Integer> map){
        String outPath = "/Users/shaozhenyu/Downloads/name-all-count.txt";
        StringBuffer sb = new StringBuffer("");
        for (Map.Entry<String,Integer> entry: map.entrySet()){
            sb.append(entry.getKey());
            sb.append(",");
            sb.append(entry.getValue());
            sb.append("\r\n");
        }
        try {
            FileWriter writer = new FileWriter(outPath);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(sb.toString());
            bw.close();
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }


    }

    public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
                oriMap.entrySet());
        Collections.sort(entryList, new MapValueComparator());

        Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
        Map.Entry<String, Integer> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }


    public void writeStatusList(Map<String,Integer> map){
        String outPath = "/Users/shaozhenyu/Downloads/status-distinct.txt";
        StringBuffer sb = new StringBuffer("");
        for (Map.Entry<String,Integer> entry: map.entrySet()){
            sb.append(entry.getKey());
            sb.append("\r\n");
        }
        try {
            FileWriter writer = new FileWriter(outPath);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(sb.toString());
            bw.close();
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
